package com.example.demo.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
//@TypeHint(types = User.class, access = AccessBits.LOAD_AND_CONSTRUCT | AccessBits.PUBLIC_METHODS)
@RequestMapping("/api")
public class UserController {

    private final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserRepository userRepository;
//
//    @Autowired
//    private UserMapper userMapper;


    @GetMapping("/index")
    @ResponseBody
    public User index() {
        Random r = new Random();
        return userRepository.findById(1L).get();
    }


    @GetMapping("/add")
    public String add() {

        User user = new User();
        user.setName("xiao名");
        user.setAge(12);
        userRepository.save(user);
        return "添加成功";
    }


    @GetMapping("/update")
    public String update() {
//        List<User> userList = userRepository.top5(1L, 100L);
//        List<User> userList = userRepository.findWithBetween(0L,100L);
        List<User> userList = userRepository.findAll();
        List<User> userList1 = new ArrayList<>();
        userList.forEach(
                user -> {
                    Random rand = new Random();
                    user.setAge(rand.nextInt(100) + 1);
                    userList1.add(user);
                }
        );
        userRepository.saveAll(userList1);
        return "修改成功";
    }

    @GetMapping("/list")
    @ResponseBody
//    public List<UserDTO> list() {
    public List<User> list() {
//        List<User> userList =   userRepository.top5(1L, 100L);
//        List<User> userList = userRepository.findWithBetween(0L,100L);
        List<User> userList = userRepository.findAll();
//        List<UserDTO> userDTOList = userList
//                .stream()
//                .map(user -> userMapper.toDto(user))
//                .collect(Collectors.toList());

        return userList;
    }
}
